package com.sxsihe.heartmate;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import cn.pedant.SweetAlert.widget.SweetAlertDialog;
import com.fourmob.datetimepicker.date.DatePickerDialog;
import com.sxsihe.heartmate.util.HttpPostUtil;
import com.sxsihe.heartmate.util.SharePreferenceUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

//public class UserInfoView extends FrameLayout  {
public class UserInfoView extends FrameLayout implements DatePickerDialog.OnDateSetListener  {
    Button submit,logout;
    SharePreferenceUtil spu;
    EditText username,password,gender,linkman,linkmanName;
    TextView phone,birthday,account;
    DatePickerDialog datePickerDialog;
    Calendar calendar = Calendar.getInstance();
    String id;
    Boolean isman = true;
    RelativeLayout user_info_detail,feedback_layout;


    public UserInfoView(final Context context) {
        super(context);
        if (spu == null) {
            spu = SharePreferenceUtil.getInstance(context);
        }
        int version = Integer.valueOf(android.os.Build.VERSION.SDK);

        View view = LayoutInflater.from(context).inflate(R.layout.main_tab_userinfo, this);
//        getUserInfo();
        account = (TextView)view.findViewById(R.id.account);
        account.setText(spu.get("phone", "           "));
        user_info_detail = (RelativeLayout)view.findViewById(R.id.user_info_detail);
        user_info_detail.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, UserInfoDetailActivity.class);
                context.startActivity(intent);
                int version = Integer.valueOf(android.os.Build.VERSION.SDK);
                if(version  >= 5) {
                    ((Activity)context).overridePendingTransition(R.anim.in_right, R.anim.out_left);
                }
            }
        });
//        feedback_layout = (RelativeLayout)view.findViewById(R.id.feedback_layout);
//        feedback_layout.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent();
//                intent.setClass(context, UserInfoFeedbackActivity.class);
//                context.startActivity(intent);
//                int version = Integer.valueOf(android.os.Build.VERSION.SDK);
//                if(version  >= 5) {
//                    ((Activity)context).overridePendingTransition(R.anim.in_right, R.anim.out_left);
//                }
//            }
//        });
        submit = (Button)view.findViewById(R.id.submit);
    }

    public void disableInput(){
        username.setEnabled(false);
        password.setEnabled(false);
         linkman.setEnabled(false);
        linkmanName.setEnabled(false);
    }
    public void enableInput(){
        username.setEnabled(true);
        password.setEnabled(true);
         linkman.setEnabled(true);
        linkmanName.setEnabled(true);
    }

    public void refreshUserInfo(String result){
        try {
            JSONObject json = new JSONObject(result);
            JSONObject msg = json.getJSONObject("msg");
            username.setText(msg.getString("name"));
            phone.setText(msg.getString("phone"));
            password.setText(msg.getString("password"));
            birthday.setText(msg.getString("birthday"));
            account.setText(msg.getString("phone"));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            try {
                date = sdf.parse(msg.getString("birthday"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            calendar.setTime(date);
            datePickerDialog.initialize(this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), false);

            linkman.setText(msg.getString("linkman"));
            linkmanName.setText(msg.getString("linkmanname"));
            id = msg.getString("id");
            submit.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((MainActivity) UserInfoView.this.getContext()).showWaitting();
                    if (spu.get("server_ip", "").equals("")) {
                        ((MainActivity) UserInfoView.this.getContext()).hideWaitting();
                        new SweetAlertDialog(UserInfoView.this.getContext(), SweetAlertDialog.ERROR_TYPE)
                                .setTitleText("出错了")
                                .setContentText("请填入正确的服务地址")
                                .show();
                        return;
                    }
                    String url = "http://" + spu.get("server_ip", "") + ":" + spu.get("server_port", "") + "/zhcjhy/loginAction.do?action=mobileUpdateUserInfo";
                    String args[] = {url,
                            id,
                            username.getText().toString(),
                            password.getText().toString(),
                            birthday.getText().toString(),
                            isman? "1" : "0",
                            linkman.getText().toString(),
                            linkmanName.getText().toString()
                    };
                    HttpPostUtil post = new HttpPostUtil(UserInfoView.this.getContext(), "updateUserInfo");
                    post.execute(args);
                }
            });
            enableInput();
        } catch (JSONException e) {
            e.printStackTrace();
            disableInput();
            submit.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(UserInfoView.this.getContext(), UserLoginActivity.class);
                    UserInfoView.this.getContext().startActivity(intent);
                }
            });
        }



    }

    public void getUserInfo(){
//        String url = "http://"+spu.get("server_ip","192.168.1.102")+":"+spu.get("server_port","8080")+"/zhcjhy/loginAction.do?action=mobilelogin";
        String url = "http://"+spu.get("server_ip","");
        if(spu.get("server_port","").equals("")){
            return;
//            url +=":"+spu.get("server_port","");
        }
        url +="/zhcjhy/loginAction.do?action=mobilelogin";
        HttpPostUtil post = new HttpPostUtil(getContext(),"getUserInfo");
        post.execute(url);
        account.setText(spu.get("phone", "           "));
    }

    @Override
    public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
        birthday.setText(year+"-"+(month+1)+"-"+day);
    }
}
